<template>
  <div class="goods-sku">
    <dl v-for="(spec, idx) in goodsData.specs" :key="idx">
      <dt>{{ spec.name }}</dt>
      <dd>
        <template v-for="value in spec.values" :key="value.name">
          <img
            @click="clickSpecs(value, spec.values)"
            v-if="value.picture"
            :class="{ selected: value.selected, disabled: value.disabled }"
            :src="value.picture"
            :title="value.name"
          />
          <span
            v-else
            @click="clickSpecs(value, spec.values)"
            :class="{ selected: value.selected, disabled: value.disabled }"
            >{{ value.name }}</span
          >
        </template>
      </dd>
    </dl>
    <!-- <dl>
      <dt>尺寸</dt>
      <dd>
        <span class="disabled">10英寸</span>
        <span class="selected">20英寸</span>
        <span>30英寸</span>
      </dd>
    </dl>
    <dl>
      <dt>版本</dt>
      <dd>
        <span>美版</span>
        <span>港版</span>
      </dd>
    </dl> -->
  </div>
</template>
<script>
// import { ref } from 'vue'
import bwPowerSet from '@/vendor/power-set'
const spliter = '★'
// 根据skus数据得到路径字典对象
const getPathMap = (skus) => {
  // console.log(skus.forEach(it => { console.log(it) }))
  const pathMap = {}
  skus.forEach(sku => {
    // 1. 过滤出有库存有效的sku
    if (sku.inventory) {
      // 2. 得到sku属性值数组
      const specs = sku.specs.map(spec => spec.valueName)
      // 3. 得到sku属性值数组的子集
      const powerSet = bwPowerSet(specs)
      // 4. 设置给路径字典对象
      powerSet.forEach(set => {
        const key = set.join(spliter)
        if (pathMap[key]) {
          // 已经有key往数组追加
          pathMap[key].push(sku.id)
        } else {
          // 没有key设置一个数组
          pathMap[key] = [sku.id]
        }
      })
    }
  })
  return pathMap
}

// 1.获取用户已经选中的条件
const getSelectedArr = specs => {
  return specs.map(spec => {
    // 找到这个属性下，用户的选择
    const value = spec.values.find(it => it.selected === true)

    return value ? value.name : undefined
  })
  // return [undefined,'中国',undefined]
}
// 2.对于每个按钮来说：在组合当前的按钮对应的
// 更新按钮的禁用状态
const updateDisabledStatus = (pathMap, specs) => {
  // 用户的选择[undefined,'中国',undefined]
  const _selectedArr = getSelectedArr(specs)

  specs.forEach((spec, idx) => {
    const selectedArr = [..._selectedArr]
    spec.values.forEach(btn => {
      // 已经选中的
      if (btn.name === selectedArr[idx]) { return }
      // 将最后一选项填入用户选择的最后一项
      selectedArr[idx] = btn.name

      // 去掉undefined拼接字符串,再去查询
      const key = selectedArr.filter(v => v).join(spliter)
      // 若找不到 设置为true
      btn.disabled = !pathMap[key] // （undefined取反）
    })
  })
}

// 根据skuId还原用户选中的规格
const initSelectedStatus = (goodsData, skuId) => {
  // 1.找到选中的具体的规格
  const sku = goodsData.skus.find(sku => sku.id === skuId)
  if (sku) {
    const selectArr = sku.specs.map(it => it.valueName)
    goodsData.specs.forEach((spec, idx) => {
      spec.values.forEach(value => {
        value.selected = (value.name === selectArr[idx])
      })
    })
  }
  // 2.设置对应的按钮selected为true
}
export default {
  name: 'GoodsSku',
  props: {
    goodsData: {
      type: Object,
      default: () => ({
        specs: [],
        skus: []
      })
    },
    skuId: {
      type: String,
      default: ''
    }
  },
  setup (props, { emit }) {
    const clickSpecs = (value, values) => {
      // 如果是禁用状态则不做处理
      if (value.disabled) { return }
      if (value.selected) {
        value.selected = false // 已选中改为未选中
      } else {
        // 把兄弟全改为未选中
        values.forEach(it => { it.selected = false })
        value.selected = true // 自己：未选中改为已选中
      }
      updateDisabledStatus(pathMap, props.goodsData.specs)
      // 向父组件抛出事件更新skuId
      tryEmit()
    }

    // 向父组件抛出事件更新skuId
    const tryEmit = () => {
      // 触发change事件将sku数据传递出去
      const selectedArr = getSelectedArr(props.goodsData.specs).filter(v => v)
      if (selectedArr.length === props.goodsData.specs.length) {
        const skuIds = pathMap[selectedArr.join(spliter)]
        const sku = props.goodsData.skus.find(sku => sku.id === skuIds[0])
        // 传递
        emit('change', {
          skuId: sku.id,
          price: sku.price,
          oldPrice: sku.oldPrice,
          inventory: sku.inventory,
          specsText: sku.specs.reduce((p, n) => `${p} ${n.name}：${n.valueName}`, '').replace(' ', '')
        })
      } else {
        emit('change', {})
      }
    }

    // 根据skuId还原用户选中的规格
    initSelectedStatus(props.goodsData, props.skuId)
    // 生成字典
    const pathMap = getPathMap(props.goodsData.skus)

    updateDisabledStatus(pathMap, props.goodsData.specs)

    return { clickSpecs }
  }
}
</script>
<style scoped lang="less">
.sku-state-mixin () {
  border: 1px solid #e4e4e4;
  margin-right: 10px;
  cursor: pointer;
  &.selected {
    border-color: @xtxColor;
  }
  &.disabled {
    opacity: 0.6;
    border-style: dashed;
    cursor: not-allowed;
  }
}
.goods-sku {
  padding-left: 10px;
  padding-top: 20px;
  dl {
    display: flex;
    padding-bottom: 20px;
    align-items: center;
    dt {
      width: 50px;
      color: #999;
    }
    dd {
      flex: 1;
      color: #666;
      > img {
        width: 50px;
        height: 50px;
        .sku-state-mixin ();
      }
      > span {
        display: inline-block;
        height: 30px;
        line-height: 28px;
        padding: 0 20px;
        .sku-state-mixin ();
      }
    }
  }
}
</style>
